class ProductsController < ApplicationController
  def index
    session[:selected_categories] = Array.new if session[:selected_categories].blank? || params[:all].present?

    if signed_in? && !current_user.admin? && current_user.client.kvn_branch
      session[:selected_brand] = Brand.where("name = 'Kevingston'").first.id
    elsif session[:selected_brand].blank? || params[:all].present?
      session[:selected_brand] = nil
    end

    #unless selected_categories.blank?
    #  @products = Product.filter_by_categories(selected_categories).paginate(:page => params[:page])
    #else
    #  @products = Product.paginate(:page => params[:page])
    #end
    @products = Product.filter({:brand_id => selected_brand, :category_ids => selected_categories}).includes(:categories).includes(:product_colors).includes(:brand).paginate(:page => params[:page])

    @categories = Category.all
    @brands = Brand.all

    respond_to do |format|
      format.js
      format.html
    end
  end

  def show
    begin
      @product = Product.find(params[:id])
      @order_ttl = configatron.order_ttl_days

      @product_page = params[:page]
    rescue
    end

    respond_to do |format|
      unless @product.blank?
        format.html
      else
        format.html { redirect_to root_url }
      end
    end

  end

  def show_cart
    @cart = current_cart
    @show_alert = params[:show_alert].present? ? true : false

    respond_to do |format|
      format.html
    end
  end

  def filter_products
    @category = params[:category]
    @brand = params[:brand]
    manage_selected_categories(@category.to_i) unless @category.blank?
    manage_selected_brand(@brand.to_i) unless @brand.blank?

    #if !selected_categories.blank?
    #  @products = Product.filter_by_categories(selected_categories).paginate(:page => params[:page])
    #elsif !brand.blank?
    #  @products = Product.filter_by_brand(brand).paginate(:page => params[:page])
    #else
    #  @products = Product.paginate(:page => params[:page])
    #end

    @products = Product.filter({:brand_id => selected_brand, :category_ids => selected_categories}).paginate(:page => params[:page])

    respond_to do |format|
      format.js
    end
  end

  def add_to_cart
    respond_to do |format|
      begin
        product_id = params[:product_id]
        #color_id = params[:color_id]
        #quantity = params[:quantity]
        items = params[:items]
        #if !product_id.blank? && !color_id.blank? && !quantity.blank? && quantity.to_i > 0
        unless items.blank?
          parsed_items = parse_cart_items(product_id, items)

          unless parsed_items.blank?
            current_cart.add_items(parsed_items)
            #item = CartItem.new(product_id, color_id, quantity)
            #current_cart.add_item(item)
            format.js
          else
            format.js { render :action => 'add_to_cart_errors' }
          end
        else
          format.js { render :action => 'add_to_cart_errors' }
        end
      rescue
        format.js { render :action => 'add_to_cart_errors' }
      end
    end
  end

  def remove_from_cart
    @cart = current_cart.remove_item(params[:item_id])

    respond_to do |format|
      format.js
    end
  end

  def empty_cart
    current_cart.empty

    respond_to do |format|
      format.js
    end
  end

  def change_item_quantity
    @cart = current_cart
    @item = current_cart.get_item(params[:item_id])
    @item.update_quantity(params[:quantity], @cart.client_id)

    respond_to do |format|
      format.js
    end
  end

  def search
    keywords = params[:keywords]

    @products = Product.search(keywords).paginate(:page => params[:page])

    respond_to do |format|
      format.html
    end
  end

  private

  def selected_categories
    session[:selected_categories]
  end

  def selected_brand
    session[:selected_brand]
  end

  def manage_selected_categories(category)
    categories = session[:selected_categories]

    if !categories.blank? && categories.include?(category)
      categories.delete(category)
      @selected = false
    else
      categories << category
      @selected = true
    end

    session[:selected_categories] = categories
  end

  def manage_selected_brand(brand_id)
    selected_brand_id = session[:selected_brand]

    if selected_brand_id != brand_id
      session[:selected_brand] = brand_id
    else
      session[:selected_brand] = nil
    end

    session[:selected_brand]
  end

  def parse_cart_items(product_id, items)
    arr_items = JSON.parse(items)
    parsed_items = Array.new

    arr_items.each do |item|
      color_id = item['color_id'].blank? ? nil : item['color_id'].to_i
      parsed_items << CartItem.new(product_id.to_i, color_id, item['quantity'].to_i)
    end

    parsed_items
  end
end
